package com.youyi.yyfilepickerlibrary.core.activity;

import android.content.Intent;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lkx.liandianqinew.R;
import com.youyi.yyfilepickerlibrary.core.SelectOptions;
import com.youyi.yyfilepickerlibrary.core.adapter.BuketAdapter;
import com.youyi.yyfilepickerlibrary.core.adapter.EssMediaAdapter;
import com.youyi.yyfilepickerlibrary.core.loader.EssAlbumCollection;
import com.youyi.yyfilepickerlibrary.core.loader.EssMediaCollection;
import com.youyi.yyfilepickerlibrary.core.model.Album;
import com.youyi.yyfilepickerlibrary.core.model.EssFile;
import com.youyi.yyfilepickerlibrary.core.util.Const;
import com.youyi.yyfilepickerlibrary.core.util.UiUtils;
import com.youyi.yyfilepickerlibrary.core.widget.MediaItemDecoration;
import com.youyi.yyfilepickerlibrary.core.widget.ToolbarSpinner;
import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import top.zibin.luban.Luban;
import top.zibin.luban.OnCompressListener;

public class SelectPictureActivity extends AppCompatActivity implements EssAlbumCollection.EssAlbumCallbacks, AdapterView.OnItemSelectedListener, EssMediaCollection.EssMediaCallbacks, BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener {
    private final EssAlbumCollection mAlbumCollection = new EssAlbumCollection();
    private BuketAdapter mBuketAdapter;
    private boolean mCanPreview = true;
    private MenuItem mCountMenuItem;
    private int mMaxCount = 10;
    private EssMediaAdapter mMediaAdapter;
    private final EssMediaCollection mMediaCollection = new EssMediaCollection();
    private boolean mNeedCamera = true;
    private boolean mNeedClip = false;
    private boolean mNeedCompress = true;
    private RecyclerView mRecyclerView;
    private Set<EssFile> mSelectedFileList = new LinkedHashSet();
    private TextView mTvSelectedFolder;
    private boolean mUseCustomCamera = true;

    @Override // com.youyi.yyfilepickerlibrary.core.loader.EssAlbumCollection.EssAlbumCallbacks
    public void onAlbumMediaReset() {
    }

    @Override // android.widget.AdapterView.OnItemSelectedListener
    public void onNothingSelected(AdapterView<?> adapterView) {
    }

    @Override // com.youyi.yyfilepickerlibrary.core.loader.EssMediaCollection.EssMediaCallbacks
    public void onmMediaReset() {
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v4.app.SupportActivity, android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onCreate(Bundle bundle) {
        setTheme(SelectOptions.getInstance().themeId);
        super.onCreate(bundle);
        setContentView(R.layout.activity_select_picture);
        this.mRecyclerView = (RecyclerView) findViewById(R.id.rcv_file_picture_list);
        this.mTvSelectedFolder = (TextView) findViewById(R.id.selected_folder);
        initUI();
    }

    private void initUI() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            supportActionBar.setDisplayShowTitleEnabled(false);
            supportActionBar.setDisplayHomeAsUpEnabled(true);
        }
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                SelectPictureActivity.this.onBackPressed();
            }
        });
        Drawable navigationIcon = toolbar.getNavigationIcon();
        TypedArray obtainStyledAttributes = getTheme().obtainStyledAttributes(new int[]{R.attr.album_element_color});
        int color = obtainStyledAttributes.getColor(0, 0);
        obtainStyledAttributes.recycle();
        if (navigationIcon != null) {
            navigationIcon.setColorFilter(color, PorterDuff.Mode.SRC_IN);
        }
        this.mBuketAdapter = new BuketAdapter(this, null, false);
        ToolbarSpinner toolbarSpinner = new ToolbarSpinner(this);
        toolbarSpinner.setSelectedTextView((TextView) findViewById(R.id.selected_folder));
        toolbarSpinner.setPopupAnchorView(findViewById(R.id.toolbar));
        toolbarSpinner.setOnItemSelectedListener(this);
        toolbarSpinner.setAdapter(this.mBuketAdapter);
        this.mAlbumCollection.onCreate(this, this);
        this.mAlbumCollection.load();
        this.mMediaCollection.onCreate(this, this);
        this.mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        this.mRecyclerView.addItemDecoration(new MediaItemDecoration());
        EssMediaAdapter essMediaAdapter = new EssMediaAdapter(new ArrayList());
        this.mMediaAdapter = essMediaAdapter;
        essMediaAdapter.setImageResize(UiUtils.getImageResize(this, this.mRecyclerView));
        this.mRecyclerView.setAdapter(this.mMediaAdapter);
        this.mMediaAdapter.bindToRecyclerView(this.mRecyclerView);
        this.mMediaAdapter.setOnItemChildClickListener(this);
        if (SelectOptions.getInstance().isSingle || SelectOptions.getInstance().maxCount == 1) {
            this.mMediaAdapter.setOnItemClickListener(this);
        }
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.media_menu, menu);
        MenuItem findItem = menu.findItem(R.id.browser_select_count);
        this.mCountMenuItem = findItem;
        findItem.setTitle(String.format(getString(R.string.selected_file_count), String.valueOf(this.mSelectedFileList.size()), String.valueOf(this.mMaxCount)));
        return true;
    }

    /* access modifiers changed from: protected */
    @Override // android.support.v7.app.AppCompatActivity, android.support.v4.app.FragmentActivity
    public void onDestroy() {
        super.onDestroy();
        this.mAlbumCollection.onDestroy();
        this.mMediaCollection.onDestroy();
    }

    @Override // com.youyi.yyfilepickerlibrary.core.loader.EssAlbumCollection.EssAlbumCallbacks
    public void onAlbumMediaLoad(Cursor cursor) {
        this.mBuketAdapter.swapCursor(cursor);
        cursor.moveToFirst();
        this.mMediaCollection.load(Album.valueOf(cursor), this.mNeedCamera, this.mSelectedFileList);
    }

    @Override // android.widget.AdapterView.OnItemSelectedListener
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long j) {
        this.mBuketAdapter.getCursor().moveToPosition(i);
        this.mMediaCollection.load(Album.valueOf(this.mBuketAdapter.getCursor()), this.mNeedCamera, this.mSelectedFileList);
    }

    @Override // com.youyi.yyfilepickerlibrary.core.loader.EssMediaCollection.EssMediaCallbacks
    public void onMediaLoad(List<EssFile> list) {
        this.mMediaAdapter.setNewData(list);
        if (list == null || list.isEmpty()) {
            this.mMediaAdapter.setEmptyView(R.layout.empty_file_list);
        }
    }

    @Override // com.chad.library.adapter.base.BaseQuickAdapter.OnItemChildClickListener
    public void onItemChildClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
        EssFile essFile = (EssFile) this.mMediaAdapter.getItem(i);
        if (baseQuickAdapter.equals(this.mMediaAdapter)) {
            if (view.getId() != R.id.check_view && view.getId() != R.id.media_thumbnail) {
                view.getId();
                int i2 = R.id.capture;
            } else if (this.mSelectedFileList.size() < SelectOptions.getInstance().maxCount || essFile.isChecked()) {
                if (this.mSelectedFileList.add((EssFile) this.mMediaAdapter.getItem(i))) {
                    ((EssFile) this.mMediaAdapter.getData().get(i)).setChecked(true);
                } else {
                    this.mSelectedFileList.remove(essFile);
                    ((EssFile) this.mMediaAdapter.getData().get(i)).setChecked(false);
                }
                this.mMediaAdapter.notifyItemChanged(i, "");
                this.mCountMenuItem.setTitle(String.format(getString(R.string.selected_file_count), String.valueOf(this.mSelectedFileList.size()), String.valueOf(this.mMaxCount)));
            } else {
                this.mMediaAdapter.notifyItemChanged(i, "");
                RecyclerView recyclerView = this.mRecyclerView;
                Snackbar.make(recyclerView, "您最多只能选择" + SelectOptions.getInstance().maxCount + "个。", -1).show();
            }
        }
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != R.id.browser_select_count || this.mSelectedFileList.isEmpty()) {
            return true;
        }
        if (SelectOptions.getInstance().compressImage) {
            final ArrayList<String> filePathList = EssFile.getFilePathList(EssFile.getEssFileList(this, this.mSelectedFileList));
            final int[] iArr = {0};
            Luban.with(this).load(filePathList).ignoreBy(100).setTargetDir(SelectOptions.getInstance().getTargetPath()).setCompressListener(new OnCompressListener() {
                /* class com.youyi.yyfilepickerlibrary.core.activity.SelectPictureActivity.AnonymousClass2 */

                @Override // top.zibin.luban.OnCompressListener
                public void onStart() {
                }

                @Override // top.zibin.luban.OnCompressListener
                public void onSuccess(File file) {
                    iArr[0] = iArr[0] + 1;
                    if (iArr[0] == filePathList.size()) {
                        try {
                            Intent intent = new Intent();
                            SelectPictureActivity selectPictureActivity = SelectPictureActivity.this;
                            intent.putParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION, EssFile.getEssFileList(selectPictureActivity, selectPictureActivity.mSelectedFileList));
                            SelectPictureActivity.this.setResult(-1, intent);
                            SelectPictureActivity.this.onBackPressed();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

                @Override // top.zibin.luban.OnCompressListener
                public void onError(Throwable th) {
                    Log.i("TAG", th.toString());
                }
            }).launch();
        } else {
            Intent intent = new Intent();
            intent.putParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION, EssFile.getEssFileList(this, this.mSelectedFileList));
            setResult(-1, intent);
            onBackPressed();
        }
        return true;
    }

    @Override // com.chad.library.adapter.base.BaseQuickAdapter.OnItemClickListener
    public void onItemClick(BaseQuickAdapter baseQuickAdapter, View view, int i) {
        this.mSelectedFileList.add((EssFile) this.mMediaAdapter.getData().get(i));
        Intent intent = new Intent();
        intent.putParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION, EssFile.getEssFileList(this, this.mSelectedFileList));
        setResult(-1, intent);
        super.onBackPressed();
    }
}
